package com.bdth.weget.net.service;

import com.bdth.weget.net.base.SessionData;
import com.bdth.weget.net.protocol.*;
import com.bdth.weget.vo.*;

public class ProtocolService {
    /**
     * JT8001
     * 创建通用应答
     * @param jtBytes
     * @param result
     * @return
     */
    public static byte[] createJT8001(JTBytes jtBytes,byte result){
        JT_8001 responseJT8100 = new JT_8001(jtBytes.getHeader().getSeriaNo(), jtBytes.getHeader().getMsgType(), result);
        return JTFactory.getJTBytes(MessageType.JT_8001,responseJT8100,jtBytes.getHeader().getSimno());
    }

    /**
     * 终端y应答解码
     * @param jtBytes
     * @return
     */
    public static byte[] protocolJT0001(JTBytes jtBytes){
        byte[] reponse = null;
        try {
            byte[] body = jtBytes.getBody();
            JT_0001 bean = new JT_0001();
            bean.read(body);
        }catch (Exception e){
            reponse = createJT8001(jtBytes,(byte)0x01);
        }
        if(reponse==null)
            reponse = createJT8001(jtBytes,(byte)0x00);
        return reponse;
    }

    /**
     * 终端注册解码
     * @param jtBytes
     * @return
     */
    public static byte[] protocolJT0100(JTBytes jtBytes){
        JT_8100 reponse = null;
        try {
            byte[] body = jtBytes.getBody();
            JT_0100 bean = new JT_0100();
            bean.read(body);
        }catch (Exception e){
            reponse = new JT_8100(jtBytes.getHeader().getSeriaNo(),(byte)0x00, SessionData.REGISTERNO);
            return JTFactory.getJTBytes(MessageType.JT_8100,reponse,jtBytes.getHeader().getSimno());
        }
        reponse = new JT_8100(jtBytes.getHeader().getSeriaNo(),(byte)0x00, SessionData.REGISTERNO);
        return JTFactory.getJTBytes(MessageType.JT_8100,reponse,jtBytes.getHeader().getSimno());
    }

    /**
     * 终端鉴权解析
     * @param jtBytes
     * @return
     */
    public static byte[] protocolJT0102(JTBytes jtBytes){
        byte[] reponse = null;
        try {
            byte[] body = jtBytes.getBody();
            JT_0102 bean = new JT_0102();
            bean.read(body);
            System.out.println(bean.toString());
            if(bean.getRegisterNo().equals(SessionData.REGISTERNO)){
                reponse = createJT8001(jtBytes,(byte)0x00);
            }else{
                reponse = createJT8001(jtBytes,(byte)0x01);
            }
        }catch (Exception e){
            reponse = createJT8001(jtBytes,(byte)0x01);
        }
        if(reponse==null)
            reponse = createJT8001(jtBytes,(byte)0x00);
        return reponse;
    }

    /**
     * 定位信息解码
     * @param jtBytes
     * @return
     */
    public static byte[] protocolJT0200(SenderService senderService,JTBytes jtBytes){
        byte[] reponse = null;
        try {
            byte[] body = jtBytes.getBody();
            JT_0200 bean = new JT_0200();
            bean.read(body);
            Location location = new Location(bean.getLatitude(),bean.getLongitude(),bean.getAltitude(),bean.getSpeed(),bean.getCourse());
            Warnning warnning = new Warnning(bean.getAlarmFlag());
            Status status = new Status(bean.getStatus());
            Expand expand = new Expand(bean.getExpand());
            JT0200 jt0200 = new JT0200(jtBytes.getHeader().getSimno(),bean.getTime(),location,warnning,status,expand);
            senderService.send(jt0200);
        }catch (Exception e){
            reponse =  createJT8001(jtBytes,(byte)0x01);
        }
        if(reponse==null)
            reponse = createJT8001(jtBytes,(byte)0x00);
        return reponse;
    }

    /**
     * 定位信息补报
     * @param jtBytes
     * @return
     */
    public static byte[] protocolJT0704(JTBytes jtBytes){
        byte[] reponse = null;
        try {
            byte[] body = jtBytes.getBody();
            JT_0704 bean = new JT_0704();
            bean.read(body);
        }catch (Exception e){
            reponse =  createJT8001(jtBytes,(byte)0x01);
        }
        if(reponse==null)
            reponse = createJT8001(jtBytes,(byte)0x00);
        return reponse;
    }
}
